package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.util.JsonUtil;
import com.example.myapplication.util.OkHttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AddUserActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);
        EditText editText1= findViewById(R.id.et1);
        EditText editText2= findViewById(R.id.et2);
        EditText editText3= findViewById(R.id.et3);
        Button button =findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText1.length()!=0 && editText2.length()!=0 && editText3.length()!=0){
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("name",editText1.getText().toString());
                    map.put("age",editText2.getText().toString());
                    map.put("phone",editText3.getText().toString());
                    String json = null;

                    try {
                        json = JsonUtil.getJson(map);
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                    if(json != null){
                        String finalJson = json;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                OkHttpUtil.okPost("/addUser", finalJson, new Callback() {
                                    @Override
                                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(AddUserActivity.this,"添加失败",Toast.LENGTH_SHORT);
                                            }
                                        });

                                    }

                                    @Override
                                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                        Intent intent = new Intent(AddUserActivity.this, HomeActivity.class);
                                        startActivity(intent);
                                    }
                                });
                            }
                        }).start();
                    }
                }else {
                    Toast.makeText(AddUserActivity.this, "请输入 再提交", Toast.LENGTH_SHORT).show();
                }


            }
        });

    }
}